package com.sfac.javaSpringBoot.modules.account.dao;

import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.common.entity.Search;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserDao {

    @Insert("insert into account_user (user_name, password, email, user_image, create_date)" +
            " values(#{userName}, #{password}, #{email}, #{userImage}, #{createDate})")
    @Options(useGeneratedKeys=true, keyProperty = "id", keyColumn = "id")
    void insertUser(User user);

    @Select("select * from account_user where user_name = #{userName} or email = #{email}")
    List<User> getUsersByUserNameOrEmail(@Param("userName") String userName,
                                         @Param("email") String email);

    @Update("update account_user set user_name = #{userName}, "+
            "email = #{email}, user_image=#{userImage} where id=#{id}")
    void updateUser(User user);

    //根据id查询
    @Select("select * from account_user where id = #{id}")
    User getUserById(int id);

    @Delete("delete from account_user where id  = #{id}")
    void deleteUserById(int id);

    //分页
    @Select("<script>"
            + "select * from account_user "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (email like '%${keyword}%' or user_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<User> getUsersBySearch(Search search);

    @Select("select *from account_user where user_name=#{username} and password =#{password}")
    User getUserByUserNameAndPassword(
            @Param("username") String username,
            @Param("password") String password
    );

}